<?php
namespace api\controllers;

use api\common\controllers\BaseController;
use api\models\TTest;

class TestController extends BaseController {


    /**
     * 向数据库中添加数据
     * @return array
     * @throws \Throwable
     */
    public function actionAdd(){
        /** @var $data 获取传过来的参数 */
        $data = \Yii::$app->request->post();
        /** @var $model \api\models\TTest 载入models类 **/
        $model = new TTest();
        /** 添加应用场景 */
        $model->setScenario('add');
        $model::$FIELDS_KEY = 'add';
        /** 加载数据与应用场景 */
        $model->load($data,'');
        /** 验证数据 */
        if($model->validate()){
            /** 插入数据 */
            if(!$model->insert(false)){
                static::$CODE = 1003;
                static::$STATUSTEXT = '添加数据失败';
            }
        }
        return $model;
    }

    /**
     * 向数据库中删除数据
     * @return TTest|array|false|int
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDel(){
        $data = \Yii::$app->request->post();
        $model = new TTest();
        $model->setScenario('del');
        $model::$FIELDS_KEY = 'del';
        $model->load($data,'');
        if($model->validate()){
            $id = $data['id'];
            $model = $model::find()->where(['id'=>$id])->one();
            if($model!=null){
                if(!$model->delete()){
                    static::$CODE = 1004;
                }
            }else{
                static::$CODE = 1003;
                static::$STATUSTEXT = '没有该数据';
            }
        }
        return $model;
    }

    /**
     * 向数据库中修改数据
     */
    public function actionModify(){
        $data = \Yii::$app->request->post();
        $model = new TTest();
        $model->setScenario('modify');
        $model::$FIELDS_KEY = 'modify';
        $model->load($data,'');
        if($model->validate()){
            $id = $data['id'];
            $model = $model::find()->where(['id'=>$id])->one();
            if($model!=null){
                $model->remark = $data['remark'];
                if(!$model->update(false)){
                    static::$CODE = 1001;
                    static::$STATUSTEXT = '数据未变动';
                }
            }else{
                static::$CODE = 1002;
                static::$STATUSTEXT = '没有该数据';
            }
        }
        return $model;
    }

    /**
     * 向数据库中查找数据
     */
    public function actionFind(){
        $data = \Yii::$app->request->get();
        $model = new TTest();
        $model->setScenario("find");
        $model->load($data,'');
        $model::$FIELDS_KEY = 'find';
        if($model->validate()){
            $remark = $data['remark'];
            $model = $model::find()->where(['remark'=>$remark])->all();
        }
        return $model;
    }

    /**
     * 定义请求方式
     * @return array
     */
    protected function verbs()
    {
        $verbs = parent::verbs();
        //定义add为POST请求方式
        $verbs['add'] = ['POST'];
        $verbs['del'] = ['DELETE'];
        $verbs['modify'] = ['PUT'];
        $verbs['find'] = ['GET'];
        return $verbs;
    }
}